package fgame.action
{
	import fgame.unit.Actor3D;
	
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import ricky.utils.FrameManager;
	import ricky.utils.TimeServer;
	
	/**
	 * 移动到指定的点直线移动
	 * 目标 带x,y,z的对象
	 * purpose(target:{x:Number,y:Number,z:Number})
	 */
	public class MoveToAct extends Act3D
	{
		public function MoveToAct(actor:Actor3D=null)
		{
			super(actor);
			this._animProperty="walk";
			this._types=4;
			target=new Point()
			_myPos=new Point();
		}
		
		override public function purpose(args:*):void
		{
			
			x=args.p.x;
			y=args.p.y;
			z=args.p.z;
			target.x=x;
			target.y=z;
			
			var s1:Number=args.s;
			var v1:Number=args.v;
			_myPos.x=this.graphic.x;
			_myPos.y=this.graphic.z;
			var dt:Number=this.currActor.netDelay;
			var sMe:Number=target.subtract(_myPos).length;
			
//			var str:String="目标("+int(x)+","+int(y)+","+int(z)+")," +"我的位置("+int(graphic.x)+","+int(graphic.y)+","+int(graphic.z)+"),"+
//				"本机距离："+ sMe+",客户距离："+s1+",客户速度v1："+v1+"本机网络延时："+dt;
//			Debug.traceMe(str);
			this.currActor.speed=-sMe*v1/(s1-dt*v1);
			this.currActor.graphic.setAttribute("frameSpeed",10);
//			this.graphic.frameSpeed=1;//this.currActor.speed*10;
			lookAtTarget();

		}
		
		override public function begin():void
		{
			super.begin();
			_lastFrameTime=this._beginTime;
			_lastDtTime=1000;
			if(animProperty)
				this._actor.playAnimAct(animProperty);
		}
		
		override public function action(time:Number):void
		{
			var dot:Number=time-_lastFrameTime;
			_lastFrameTime=time;
			_lastDtTime+=dot;
			if(_lastDtTime>1000)
			{
				_lastDtTime=0;
				lookAtTarget();
			}
			
			if(this.isArrive())
			{
				this.complete();
			}
			else
			{
				this.graphic.translateZ(this.currActor.speed*dot);
				super.setToSurface();
			}
		}
		
		
		
		override public function end():void
		{
			super.end();
		}
		
		protected function lookAtTarget():void
		{
			graphic.lookAt(x,y,z);
			graphic.rotateY(180);
		}

		
		/**
		 * 是否移动到目标x,y
		 */
		protected function isArrive():Boolean
		{
			_myPos.x=this.graphic.x;
			_myPos.y=this.graphic.z;
			if (target.subtract(_myPos).length<_radius)//if(x==this._currentObject.coordinate.x && this._currentObject.coordinate.y==y)
			{	
				_isArrive=true;
				return _isArrive;
			}
			else
				return false;
		}
		
		protected var _lastFrameTime:Number=0;
		
		protected var _fpsDt:Number;
		protected var _radius:Number=10;
		protected var _myPos:Point;
		protected var _lastDtTime:Number=0;

		/**  每毫秒移动距离  */
//		protected var _speed:Number=-0.10;
		protected var _isArrive:Boolean=false;

		protected var target:Point;
		protected var x:Number,y:Number,z:Number;
	}
}